package net.narusas.aceauction.push.db;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.narusas.aceauction.push.model.Device;

import org.junit.Test;

public class GoodsSearchDAOTest {

	@Test
	public void test() {
		String src = "aaa${addr}bbb${code1 code2}";
		Pattern pattern = Pattern.compile("(\\$\\{[^\\}]+\\})");
		Matcher m = pattern.matcher(src);
		assertTrue(m.find());

		assertEquals("${addr}", m.group(1));

		assertTrue(m.find());
		assertEquals("${code1 code2}", m.group(1));
	}

	@Test
	public void test2() {
		String src = "${addr}${code1 code2}";
		Pattern pattern = Pattern.compile("\\$\\{([^\\}]+)\\}");
		Matcher m = pattern.matcher(src);

		assertTrue(m.find());
		assertEquals("addr", m.group(1));

		assertTrue(m.find());
		assertEquals("code1 code2", m.group(1));
	}

	@Test
	public void generateSQL() throws IOException {
		GoodsSearchDAO dao = new GoodsSearchDAO();
		String template = "aaa${addr}bbb${code1 code2}ddd";

		Map<String, Object> map = new HashMap<String, Object>();
		assertEquals("aaabbbddd", dao.generateSQL(template, map));
		
		map.put("addr", 10);
		map.put("code1", "test1");
		assertEquals("aaa10bbbtest1ddd", dao.generateSQL(template, map));
		
		map.put("code2", "test2");
		assertEquals("aaa10bbbtest1 test2ddd", dao.generateSQL(template, map));
		

	}

}
